package com.example.delivery.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.delivery.mapper.AddressMapper;
import com.example.delivery.pojo.Address;
import com.example.delivery.pojo.User;
import com.example.delivery.service.AddressService;
import com.example.delivery.service.RestaurantService;
import com.example.delivery.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户注册
     * @param
     * @return
     */
    @PostMapping("/register")
    public ModelAndView register(String username, String password) {
        User user = new User();
        user.setUserName(username);
        user.setPassword(password);
        user.setPhone("123456");
        boolean flag = userService.registerUser(user);
        ModelAndView modelAndView = new ModelAndView();
        if(flag) { //注册成功,跳转登陆页面
            modelAndView.setViewName("/login");
        }else{ //登陆失败，给出提示信息
            modelAndView.setViewName("/register");
            modelAndView.addObject("msg","用户名已存在，请重新注册");
        }
        return modelAndView;
    }


    @Autowired
    private RestaurantService restaurantService;


    /**
     * 用户登录
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/login")
    public ModelAndView login(String username, String password, HttpSession session) {
        boolean flag  = userService.loginUser(username, password);

        ModelAndView modelAndView = new ModelAndView();
        if(!flag) {
            modelAndView.setViewName("/login");
            modelAndView.addObject("msg","用户名或密码错误，请重新登录");
            return modelAndView;
        }

        modelAndView.setViewName("/index");
        session.setAttribute("user",username);
        User user = userService.findByUserName(username);
        session.setAttribute("userId",user.getId());
        modelAndView.addObject("restaurants",restaurantService.findAll());
        return modelAndView;

    }

    @GetMapping("/logout")
    public ModelAndView logout(HttpSession session) {
        session.invalidate();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/login");
        return modelAndView;
    }
    @Autowired
    private AddressMapper addressMapper;
    @GetMapping("/checkOut")
    public String checkOut(HttpSession session, @RequestParam String totalAmount, @RequestParam String restaurantId) {
        Object userId = session.getAttribute("userId");

        session.setAttribute("totalAmount", totalAmount);
        session.setAttribute("restaurantId", restaurantId);

        // 查询出用户地址
        String username = (String) session.getAttribute("user");
        //查询用户
        QueryWrapper<User> wrapper1 = new QueryWrapper();
        wrapper1.eq("userName", username);
        User user = userService.getOne(wrapper1);
        QueryWrapper<Address> wrapper = new QueryWrapper();
        wrapper.eq("userId", user.getId());
        List<Address> userAddresses = addressMapper.selectList(wrapper);
        session.setAttribute("userAddresses", userAddresses);

        return "redirect:/payment";
    }

    @GetMapping("/profile")
    public ModelAndView profile(HttpSession session) {
        String   username = (String) session.getAttribute("user");
        User user = userService.findByUserName(username);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("user",user);
        modelAndView.setViewName("/profile");
        return modelAndView;
    }
}
